﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
 
using System.Text;
using System.Windows.Forms;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.CellType;
using FarPoint.Win;

namespace Demo
{
    public partial class hierarchy : DemoBaseUserControl
    {
        System.Data.DataSet myDataSet = new System.Data.DataSet();
        System.Drawing.Color altcolor0;
        System.Drawing.Color altcolor1;
        System.Drawing.Color childheader1;
         System.Drawing.Color childheader0;

        public hierarchy()
        {
            InitializeComponent();
            this.Title = "级联";
            this.Description = "Spread .NET  提供用“分层视图”显示关系数据功能。要设置分层数据显示，首先要创建数据集保存相关数据，" +
                "然后定义数据之间的关系，最后，设置 Spread 控件的数据来显示分层视图。";
        }

        private void hierarchy_Load(object sender, System.EventArgs e)
        {
            altcolor0 = Color.Beige;
            altcolor1 = Color.LavenderBlush;
            childheader0 = Color.DarkSeaGreen;
            childheader1 = Color.Tan;

            InitData();
            InitSpread();
        }
        public void InitData()
        {
            System.Data.DataTable fpnames;
            System.Data.DataTable fpclass;
            System.Data.DataTable fpgrades;
            fpnames = myDataSet.Tables.Add("学生");
            fpnames.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn("姓名", Type.GetType("System.String")), new System.Data.DataColumn("学号", Type.GetType("System.Int32")) });
            fpnames.Rows.Add(new Object[] { "王明", 0 });
            fpnames.Rows.Add(new Object[] { "张强", 1 });
            fpnames.Rows.Add(new Object[] { "周惠", 2 });
            fpclass = myDataSet.Tables.Add("Class");
            fpclass.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn("课程", Type.GetType("System.String")), new System.Data.DataColumn("owner", Type.GetType("System.Int32")), new System.Data.DataColumn("owner-subject", Type.GetType("System.String")) });
            //First Student
            fpclass.Rows.Add(new Object[] { "数学", 0, "0-数学" });
            fpclass.Rows.Add(new Object[] { "英语", 0, "0-英语" });
            fpclass.Rows.Add(new Object[] { "历史", 0, "0-历史" });
            fpclass.Rows.Add(new Object[] { "音乐", 0, "0-音乐" });
            fpclass.Rows.Add(new Object[] { "科学", 0, "0-科学" });
            //Second Student
            fpclass.Rows.Add(new Object[] { "数学", 1, "1-数学" });
            fpclass.Rows.Add(new Object[] { "英语", 1, "1-英语" });
            fpclass.Rows.Add(new Object[] { "历史", 1, "1-历史" });
            fpclass.Rows.Add(new Object[] { "音乐", 1, "1-音乐" });
            fpclass.Rows.Add(new Object[] { "科学", 1, "1-科学" });
            //Third Student
            fpclass.Rows.Add(new Object[] { "数学", 2, "2-数学" });
            fpclass.Rows.Add(new Object[] { "英语", 2, "2-英语" });
            fpclass.Rows.Add(new Object[] { "历史", 2, "2-历史" });
            fpclass.Rows.Add(new Object[] { "音乐", 2, "2-音乐" });
            fpclass.Rows.Add(new Object[] { "科学", 2, "2-科学" });

            fpgrades = myDataSet.Tables.Add("Grades");
            fpgrades.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn("第一周", Type.GetType("System.String")), new System.Data.DataColumn("第二周", Type.GetType("System.String")), new System.Data.DataColumn("第三周", Type.GetType("System.String")), new System.Data.DataColumn("第四周", Type.GetType("System.String")), new System.Data.DataColumn("第五周", Type.GetType("System.String")), new System.Data.DataColumn("第六周", Type.GetType("System.String")), new System.Data.DataColumn("第七周", Type.GetType("System.String")), new System.Data.DataColumn("Subject", Type.GetType("System.String")), new System.Data.DataColumn("owner-subject", Type.GetType("System.String")) });
            //First Student
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "数学", "0-数学" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "英语", "0-英语" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "历史", "0-历史" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "音乐", "0-音乐" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "科学", "0-科学" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "数学", "1-数学" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "英语", "1-英语" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "历史", "1-历史" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "音乐", "1-音乐" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "科学", "1-科学" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "数学", "2-数学" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "英语", "2-英语" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "历史", "2-历史" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "音乐", "2-音乐" });
            fpgrades.Rows.Add(new Object[] { "76", "80", "85", "89", "90", "93", "99", "科学", "2-科学" });

            //Set the relations
            myDataSet.Relations.Add("Class", fpnames.Columns["学号"], fpclass.Columns["owner"]);
            myDataSet.Relations.Add("Grades", fpclass.Columns["owner-subject"], fpgrades.Columns["owner-subject"]);
        }

        public void InitSpread()
        {
            FpSpread1.Sheets[0].OperationMode = OperationMode.ReadOnly;
            FpSpread1.Sheets[0].DataSource = myDataSet;
            FpSpread1.Sheets[0].DataMember = "Students";
            FpSpread1.Sheets[0].ExpandRow(0, true);

            FarPoint.Win.Spread.SheetView childSheet = new SheetView();
            childSheet = this.FpSpread1_Sheet1.GetChildView(0, 0);
            childSheet.ExpandRow(0, true);
            childSheet.ExpandRow(1, true);
            childSheet.ExpandRow(2, true);
            childSheet.ExpandRow(3, true);
            childSheet.ExpandRow(4, true);

            FpSpread1.Sheets[0].DefaultStyle.Font = new Font("微软雅黑", 8);
            FpSpread1.Sheets[0].ColumnHeader.DefaultStyle.Font = new Font("微软雅黑", 8);
            FpSpread1.Sheets[0].RowHeader.DefaultStyle.Font = new Font("微软雅黑", 8);
            FpSpread1.Sheets[0].AlternatingRows[0].BackColor = Color.BlueViolet;
            FpSpread1.Sheets[0].AlternatingRows[0].BackColor = altcolor0;
            FpSpread1.Sheets[0].AlternatingRows[1].BackColor = altcolor1;
            //Set width on row header to display to nav buttons
            FpSpread1.Sheets[0].Models.RowHeaderColumnAxis.SetSize(0, 40);
            //FirstName
            FpSpread1.Sheets[0].SetColumnWidth(0, 150);
            //LastName
            FpSpread1.Sheets[0].SetColumnWidth(1, 310);
        }

        public void SetChildViewStyle(FarPoint.Win.Spread.SheetView sv)
        {
            int i;
            FarPoint.Win.Spread.CellType.GeneralCellType imgType = new FarPoint.Win.Spread.CellType.GeneralCellType();
            FarPoint.Win.Spread.CellType.GeneralCellType imgType2 = new GeneralCellType();
            FarPoint.Win.Spread.CellType.GeneralCellType imgType3 = new GeneralCellType();
            FarPoint.Win.Spread.CellType.GeneralCellType imgType4 = new GeneralCellType();
            FarPoint.Win.Spread.CellType.GeneralCellType imgType5 = new GeneralCellType();

            string defaultCommonPath = System.IO.Path.Combine(Application.StartupPath, "..\\..\\Resources\\");// default XML path
            string commonPath1 = defaultCommonPath;

            Microsoft.Win32.RegistryKey commonKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("FarPoint.Win.Spread\\Resources");
            if (commonKey != null)
            {
                object commonPathObj = commonKey.GetValue("v52005", defaultCommonPath);
                commonPath1 = commonPathObj is string ? (string)commonPathObj : defaultCommonPath;
            }
            string sMath = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "math.gif");
            string sEnglish = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "english.gif");
            string sHistory = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "history.gif");
            string sMusic = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "music.gif");
            string sScience = System.IO.Path.Combine(System.IO.Path.Combine(commonPath1, "CellTypes"), "science.gif");

            //sMath = this.GetType().Assembly.GetManifestResourceStream("SpreadWinDemo.math.gif");
            imgType.BackgroundImage = new FarPoint.Win.Picture(global::Demo.Properties.Resources.math);
            imgType.CancelEditing();
            //sEnglish = this.GetType().Assembly.GetManifestResourceStream("SpreadWinDemo.english.gif");
            imgType2.BackgroundImage = new Picture(global::Demo.Properties.Resources.english);
            imgType2.CancelEditing();
            //sHistory = this.GetType().Assembly.GetManifestResourceStream("SpreadWinDemo.history.gif");
            imgType3.BackgroundImage = new Picture(global::Demo.Properties.Resources.history);
            imgType3.CancelEditing();
            //sMusic = this.GetType().Assembly.GetManifestResourceStream("SpreadWinDemo.music.gif");
            imgType4.BackgroundImage = new Picture(global::Demo.Properties.Resources.music);
            imgType4.CancelEditing();
            //sScience = this.GetType().Assembly.GetManifestResourceStream("SpreadWinDemo.science.gif");
            imgType5.BackgroundImage = new Picture(global::Demo.Properties.Resources.science);
            imgType5.CancelEditing();
            sv.DataAutoSizeColumns = false;
            sv.DataAutoCellTypes = false;

            switch (sv.ParentRelationName)
            {
                case "Class":
                    //Selected the Class (Subject) Level
                    if (sv.ColumnCount != 4)
                    {
                        //Add a column
                        sv.Columns.Add(0, 1);
                        sv.Columns[0].Width = 25;
                        sv.Columns[0].BackColor = altcolor0;
                        //'Math
                        sv.Cells[0, 0].CellType = imgType;
                        //English
                        sv.Cells[1, 0].CellType = imgType2;
                        //History
                        sv.Cells[2, 0].CellType = imgType3;
                        //Music
                        sv.Cells[3, 0].CellType = imgType4;
                        //Science
                        sv.Cells[4, 0].CellType = imgType5;
                        //Hide the ID column
                        sv.Columns[2].Visible = false;
                        sv.Columns[3].Visible = false;
                    }

                    sv.ColumnHeader.DefaultStyle.Font = new Font("微软雅黑", 8, FontStyle.Regular);
                    sv.ColumnHeader.DefaultStyle.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Left;
                    sv.ColumnHeader.DefaultStyle.BackColor = childheader0;
                    sv.ColumnHeader.DefaultStyle.Border = new EmptyBorder();
                    sv.RowHeader.DefaultStyle.BackColor = childheader0;
                    sv.RowHeader.AutoText = HeaderAutoText.Blank;
                    sv.SheetCornerStyle.BackColor = childheader0;
                    sv.SheetCornerStyle.Border = new EmptyBorder();
                    sv.Columns[1].BackColor = altcolor0;     //sv.Parent.Cells
                    sv.Columns[1].Width = 390;

                    for (i = 0; i == sv.RowCount - 1; i++)
                    {
                        sv.Rows[i].Height = 25;
                    }

                    break;

                case "Grades":
                    //Selected the Grade Level
                    //Change the column widths

                    for (i = 0; i == sv.ColumnCount - 1; i++)
                    {
                        sv.Columns[i].Width = 50;
                    }
                    //Hide the ID columns
                    sv.Columns[7].Visible = false;
                    sv.Columns[8].Visible = false;
                    //Make the header font italic
                    sv.ColumnHeader.DefaultStyle.Font = new Font("微软雅黑", 8, FontStyle.Regular);
                    sv.ColumnHeader.DefaultStyle.Border = new EmptyBorder();
                    sv.ColumnHeader.DefaultStyle.BackColor = childheader1;
                    //Change the sheet corner color
                    sv.SheetCornerStyle.BackColor = childheader1;
                    sv.SheetCornerStyle.Border = new EmptyBorder();
                    //Clear the autotext
                    sv.RowHeader.AutoText = HeaderAutoText.Blank;
                    sv.RowHeader.DefaultStyle.Border = new EmptyBorder();
                    sv.RowHeader.DefaultStyle.BackColor = childheader1;
                    for (i = 0; i == sv.RowCount - 1; i++)
                    {
                        sv.Rows[i].Height = 20;
                    }
                    break;
            }
        }

        private void FpSpread1_ChildViewCreated(object sender, FarPoint.Win.Spread.ChildViewCreatedEventArgs e)
        {
            SetChildViewStyle(e.SheetView);
        }
    }
}
